package com.qf.anli.controller;

import com.github.pagehelper.PageInfo;
import com.qf.anli.domain.R;
import com.qf.anli.domain.Student;
import com.qf.anli.service.StudentService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public R findAll(){
        List<Student> students = studentService.findAll();

        return R.success(students);
    }

    @GetMapping("/{pageNum}/{pageSize}")
    public R findPage(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){
        PageInfo<Student> pageInfo = studentService.findPage(pageNum,pageSize);

        return R.success(pageInfo);
    }

    @PutMapping
    public R update(@RequestBody Student stu){
        int count = studentService.update(stu);

        if (count > 0){
            return R.success(null,"更新成功");
        }else{
            return R.error("更新失败");
        }
    }

    @PostMapping
    public R add(@RequestBody Student stu){
        int count = studentService.add(stu);

        if (count > 0){
            return R.success(null,"新增成功");
        }else{
            return R.error("新增失败");
        }
    }

    @DeleteMapping("/{number}")
    public R del(@PathVariable("number")String number){
        int count = studentService.delete(number);
        if (count > 0){
            return R.success(null,"删除成功");
        }else{
            return R.error("删除失败");
        }
    }
}
